﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Mapster;
using Raffles.OA.Data;
using Raffles.OA.Utility;

namespace Raffles.OA.WebApp.Areas.Sale.Controllers
{
    /// <summary>
    /// 日 期：2024-07-16 14:04:51
    /// 描 述：渠道管理控制器类
    /// </summary>
    public class ChannelEntityController :  BaseController<ChannelEntity>
    {
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="param"></param>
        /// <param name="pagination"></param>
        /// <returns></returns>
        public ActionResult GetPageListJson(BaseSearchParam param, Pagination pagination)
        {
           int totalCount=0;
           var datas=_dbContext.Queryable<ChannelEntity>()
                               /*.WhereIF(!param.KeyWord.IsEmpty(), x => x.Name == param.KeyWord.Trim())*/
                               .ToPageList(pagination.PageIndex, pagination.PageSize, ref totalCount);
           return Content(DataResult<List<ChannelEntity>>.PageResult(datas, totalCount).ToJson());
        }

        /// <summary>
        /// Form表单
        /// </summary>
        /// <returns></returns>
        public ActionResult Form()
        {
          return View();
        }

        /// <summary>
        /// 表单保存
        /// </summary>
       /// <param name="channelEntityDto"></param>
        /// <returns></returns>
        [HttpPost]
       public ActionResult SaveForm(ChannelEntityDto channelEntityDto)
        {
            try
            {
               ChannelEntity entity = new ChannelEntity();
               if (channelEntityDto.Id == null)
               {
                   channelEntityDto.Adapt(entity);
                   entity.CreatedOn = DateTime.Now;
                   entity.CreatedBy = OperatorProvider.Provider.GetCurrent().Id;
                   _dbContext.Insertable<ChannelEntity>(entity).ExecuteCommand();
                   return Json(DataResult.Success("新增成功"));
               }
               else
               {
                   entity= _dbContext.Queryable<ChannelEntity>().InSingle(channelEntityDto.Id);
                   channelEntityDto.Adapt(entity);
                   entity.UpdatedOn = DateTime.Now;
                   entity.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                   _dbContext.Updateable<ChannelEntity>(entity).ExecuteCommand();
                   return Json(DataResult.Success("编辑成功"));
               }
            }
            catch (Exception ex)
            {
               return Json(DataResult.Error($"保存失败,失败原因:{ex.Message}"));
            }
        }

        /// <summary>
        /// 数据删除
        /// </summary>
       /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
       public ActionResult DeleteData(int id)
        {
            try
            {
              _dbContext.Deleteable<ChannelEntity>().In(id).ExecuteCommand();
               return Json(DataResult.Success("删除成功"));
            }
            catch (Exception ex)
            {
               return Json(DataResult.Error($"删除失败,失败原因:{ex.Message}"));
            }
        }
    }
}
